package org.goj.management.controller;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.goj.common.entity.management.Mail;
import org.goj.common.model.Result;
import org.goj.management.service.MailService;
import org.goj.management.vo.AdminMailVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 消息列表 前端控制器
 * </p>
 *
 * @author CK
 * @since 2021-04-23
 */
@RestController
@RequestMapping("/mail")
public class MailController {

    @Autowired
    private MailService mailService;

    /**
     * 管理员对竞赛发布通知
     *
     * @param contestId   竞赛ID
     * @param adminUserId 管理员ID
     * @param title       标题
     * @param content     内容
     * @return Result<Boolean>
     * @author CK
     * @date 2021/4/23 19:36
     */
    @PostMapping("/adminAddMail")
    public Result<Boolean> adminAddMail(@RequestParam(value = "contestId") Integer contestId,
                                        @RequestParam(value = "adminUserId") Integer adminUserId,
                                        @RequestParam(value = "title") String title,
                                        @RequestParam(value = "content") String content) {
        Boolean flag = mailService.adminAddMail(contestId, adminUserId, title, content);
        Assert.isTrue(flag, "添加通知失败");
        return Result.succeed("添加通知成功", true);
    }

    /**
     * 用户提出问题
     *
     * @param contestId 竞赛ID
     * @param ordUserId 用户ID
     * @param title     标题
     * @param content   内容
     * @return Result<Boolean>
     * @author CK
     * @date 2021/4/24 10:30
     */
    @PostMapping("/addMail")
    public Result<Boolean> addMail(@RequestParam(value = "contestId") Integer contestId,
                                   @RequestParam(value = "ordUserId") Integer ordUserId,
                                   @RequestParam(value = "title") String title,
                                   @RequestParam(value = "content") String content) {
        Boolean flag = mailService.addMail(contestId, ordUserId, title, content);
        Assert.isTrue(flag, "添加问题失败");
        return Result.succeed("添加问题成功", true);
    }

    /**
     * 管理员查询竞赛消息
     *
     * @param contestId 竞赛ID
     * @return Result<List < Mail>>
     * @author CK
     * @date 2021/4/23 19:51
     */
    @GetMapping("/adminMailList")
    public Result<List<AdminMailVO>> adminMailList(@RequestParam(value = "contestId") Integer contestId) {
        List<AdminMailVO> list = mailService.adminMailList(contestId);
        return Result.succeed("查询竞赛信息成功", list);
    }

    /**
     * 用户查询竞赛通知或公告
     *
     * @param contestId 竞赛ID
     * @return Result<List < Mail>>
     * @author CK
     * @date 2021/4/24 10:41
     */
    @GetMapping("/mailList")
    public Result<List<Mail>> mailList(@RequestParam(value = "contestId") Integer contestId) {
        QueryWrapper<Mail> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("contest_id", "title", "content", "answer", "type")
                .eq("contest_id", contestId)
                .eq("defunct", "N")
                .orderByDesc("in_date");
        List<Mail> list = mailService.list(queryWrapper);
        return Result.succeed("查询通知成功", list);
    }

    /**
     * 管理员修改消息状态
     *
     * @param mailId  消息ID
     * @param defunct 状态
     * @return Result<Boolean>
     * @author CK
     * @date 2021/4/24 18:19
     */
    @PostMapping("/updateMailDefunct")
    public Result<Boolean> updateMailDefunct(
            @RequestParam(value = "mailId") Integer mailId,
            @RequestParam(value = "defunct") String defunct) {
        defunct = "N".equals(defunct) ? "Y" : "N";
        Mail mail = new Mail();
        mail.setMailId(mailId).setDefunct(defunct);
        boolean flag = mailService.updateById(mail);
        Assert.isTrue(flag, "修改状态失败！");
        return Result.succeed("修改状态成功", flag);
    }

    /**
     * 管理员删除竞赛消息
     *
     * @param mailId 消息ID
     * @return Result<Boolean>
     * @author CK
     * @date 2021/4/24 18:26
     */
    @PostMapping("/deleteMail")
    public Result<Boolean> deleteMail(@RequestParam(value = "mailId") Integer mailId) {
        boolean flag = mailService.removeById(mailId);
        Assert.isTrue(flag, "删除竞赛消息失败");
        return Result.succeed("删除竞赛消息成功", flag);
    }

    /**
     * 管理员编辑前加载数据
     *
     * @param mailId 消息ID
     * @return Result<Mail>
     * @author CK
     * @date 2021/4/24 19:18
     */
    @GetMapping("/openEditMail")
    public Result<Mail> openEditMail(@RequestParam(value = "mailId") Integer mailId) {
        Mail mail = mailService.getById(mailId);
        Assert.notNull(mail, "加载消息失败");
        return Result.succeed("加载消息成功", mail);
    }

    /**
     * 修改竞赛消息的类型
     *
     * @param mailId  消息ID
     * @param userId  管理员ID
     * @param type    消息类型
     * @param answer  回复
     * @param title   标题
     * @param content 内容
     * @return Result<Boolean>
     * @author CK
     * @date 2021/4/24 19:00
     */
    @PostMapping("/editMail")
    public Result<Boolean> editMail(@RequestParam(value = "mailId") Integer mailId,
                                    @RequestParam(value = "userId") Integer userId,
                                    @RequestParam(value = "type") Integer type,
                                    @RequestParam(value = "answer", required = false) String answer,
                                    @RequestParam(value = "title", required = false) String title,
                                    @RequestParam(value = "content", required = false) String content) {
        Mail mail = new Mail();
        mail.setMailId(mailId).setAdminUserId(userId).setInDate(new Date());
        if (type == 0) {
            Assert.isTrue(StrUtil.isNotBlank(title), "标题不能为空");
            Assert.isTrue(StrUtil.isNotBlank(content), "内容不能为空");
            mail.setTitle(title).setContent(content);
        }
        if (type > 0) {
            Assert.isTrue(StrUtil.isNotBlank(answer), "回复不能为空");
            mail.setAnswer(answer);
            if (type == 2) {
                mail.setType(1);
            }
        }
        boolean flag = mailService.updateById(mail);
        Assert.isTrue(flag, "回复&修改消息失败！");
        return Result.succeed("回复&修改消息成功", flag);
    }

}

